﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DeliveryPlus.Web.Controllers;
using DeliveryPlus.Web.DAL.Entities;

namespace DeliveryPlus.Web.Areas.ControlPanel.Controllers
{
    /// <summary>
    /// Контроллер управления районами
    /// </summary>
    public class ManageDistrictsController : BaseController
    {
        /// <summary>
        /// Отображает список районов
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var districts = DataContext.Districts.OrderByDescending(a => a.Title).ToList();
            return View(districts);
        }

        /// <summary>
        /// Отображает форму добавления нового района
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View("Edit", new District());
        }

        /// <summary>
        /// Отображает форму редактирования указанного района
        /// </summary>
        /// <param name="id">Идентификатор новости</param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            var dist = DataContext.Districts.FirstOrDefault(a => a.Id == id);
            if (dist == null)
            {
                return RedirectToAction("Index");
            }

            return View("Edit", dist);
        }

        /// <summary>
        /// Удаляет указанный район
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(long id)
        {
            var dist = DataContext.Districts.FirstOrDefault(a => a.Id == id);
            if (dist == null)
            {
                return RedirectToAction("Index");
            }

            if (dist.Cafes.Count > 0)
            {
                return RedirectToAction("Index");
            }

            DataContext.Districts.DeleteOnSubmit(dist);
            DataContext.SubmitChanges();

            return RedirectToAction("Index");
        }

        /// <summary>
        /// Обрабатывает создание или сохранение района
        /// </summary>
        /// <param name="model">Модель</param>
        /// <returns></returns>
        [ValidateInput(false)]
        public ActionResult Save(District model)
        {
            if (model.Id <= 0)
            {
                model.DateCreated = DateTime.Now;
                DataContext.Districts.InsertOnSubmit(model);
                DataContext.SubmitChanges();
            }
            else
            {
                var district = DataContext.Districts.FirstOrDefault(a => a.Id == model.Id);
                if (district == null)
                {
                    return RedirectToAction("Index");
                }

                district.Title = model.Title;
                district.Description = model.Description;
                district.DateModified = model.DateModified;
                DataContext.SubmitChanges();
            }

            return RedirectToAction("Index");
        }

    }
}
